home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Toolbox / Visual Basic Toolbox (P.I.E.)(1996).ISO / boxes / demo2 / demo2.bas < prev    next >
BASIC Source File  |  1995-03-30  |  6KB  |  178 lines

  1. ' The following Declares and constants were taken from WIN30API.TXT
  2. ' that ships with the Visual Basic Professional Edition. Get used to the API
  3. ' my friends and the world is yours. I was afraid of it for way too long.
  4. '
  5. Declare Function GetWindowTextLength Lib "user" (ByVal hwnd%) As Integer
  6. Declare Function GetWindowsDirectory Lib "Kernel" (ByVal lpBuffer As String, ByVal nSize As Integer) As Integer
  7. Declare Function GetSystemDirectory Lib "Kernel" (ByVal lpBuffer As String, ByVal nSize As Integer) As Integer
  8. Declare Function GetModuleUsage Lib "Kernel" (ByVal hModule As Integer) As Integer
  9. Declare Function SetFocusAPI Lib "User" Alias "SetFocus" (ByVal hwnd As Integer) As Integer
  10. Declare Function GetDeskTopWindow Lib "User" () As Integer
  11. Declare Function GetWindow Lib "User" (ByVal hwnd As Integer, ByVal wCmd As Integer) As Integer
  12. Declare Function GetWindowText Lib "User" (ByVal hwnd As Integer, ByVal lpString As String, ByVal aint As Integer) As Integer
  13. Declare Function GetClassName Lib "User" (ByVal hwnd As Integer, ByVal lpClassName As String, ByVal nMaxCount As Integer) As Integer
  14. Declare Function GetParent Lib "User" (ByVal hwnd As Integer) As Integer
  15. Declare Function GetWindowWord Lib "User" (ByVal hwnd As Integer, ByVal nIndex As Integer) As Integer
  16. Global Const GW_HWNDNEXT = 2
  17. Global Const GW_CHILD = 5
  18. Global Const GWW_ID = (-12)
  19. Global Const SW_HIDE = 0
  20. Global Const SW_NORMAL = 1
  21. Global Const SW_MAXIMIZE = 3
  22. Global Const SW_MINIMIZE = 6
  23.  
  24. 'This does nothing except waste time while the shelled App
  25. 'is doing it's thing. I wanted to have the user modify the
  26. 'INI file using Notepad and since I needed to re-read the
  27. 'file I basically am forced to wait until the user is done.
  28. '
  29. Function DOShell (ShellString As String, WinType As Integer)
  30. Dim InstanceHandle As Integer, HowMuchUsage As Integer
  31.  
  32.     InstanceHandle = Shell(ShellString, WinType)
  33.     
  34.     Do While GetModuleUsage(InstanceHandle) > 0
  35.     HowMuchUsage = DoEvents()
  36.     Loop
  37.  
  38. End Function
  39.  
  40. 'FindWindowLike
  41.    ' - The FindWindowLike function finds the hWnds of the windows
  42.    '   matching the specified parameters
  43.    '
  44.    'hwndArray()
  45.    ' - An integer array used to return the window handles
  46.    '
  47.    'hWndStart
  48.    ' - The handle of the window to search under.
  49.    ' - The routine searches through all of this window's children and their
  50.    '   children recursively.
  51.    ' - If hWndStart = 0 then the routine searches through all windows.
  52.    '
  53.    'WindowText
  54.    ' - The pattern used with the Like operator to compare window's text.
  55.    '
  56.    'ClassName
  57.    ' - The pattern used with the Like operator to compare window's class
  58.    '   name.
  59.    '
  60.    'ID
  61.    ' - A child ID number used to identify a window.
  62.    ' - Can be a decimal number or a hex string.
  63.    ' - Prefix hex strings with "&H" or an error will occur.
  64.    ' - To ignore the ID pass the Visual Basic Null function.
  65. '
  66.    '
  67.    Function FindWindowLike (hWndArray() As Integer, ByVal hWndStart As Integer, WindowText As String, Classname As String, ID) As Integer
  68.    Dim hwnd As Integer
  69.    Dim sWindowText As String
  70.    Dim r As Integer
  71.    ' Hold the level of recursion:
  72.    Static level As Integer
  73.    ' Hold the number of matching windows:
  74.    Static iFound As Integer
  75.  
  76.    ' Initialize if necessary:
  77.    
  78.    If level = 0 Then
  79.       iFound = 0
  80.       ReDim hWndArray(0 To 0)
  81.  
  82.       If hWndStart = 0 Then hWndStart = GetDeskTopWindow()
  83.    End If
  84.  
  85.    ' Increase recursion counter:
  86.    level = level + 1
  87.  
  88.    ' Get first child window:
  89.    hwnd = GetWindow(hWndStart, GW_CHILD)
  90.  
  91.    Do Until hwnd = 0
  92.       DoEvents ' Not necessary
  93.       ' Search children by recursion:
  94.       r = FindWindowLike(hWndArray(), hwnd, WindowText, Classname, ID)
  95.  
  96.       ' Get the window text and class name:
  97.       sWindowText = Space(255)
  98.       r = GetWindowText(hwnd, sWindowText, 255)
  99.       sWindowText = Left(sWindowText, r)
  100.  
  101.       sClassname = Space(255)
  102.       r = GetClassName(hwnd, sClassname, 255)
  103.       sClassname = Left(sClassname, r)
  104.  
  105.       ' If window is a child get the ID:
  106.       If GetParent(hwnd) <> 0 Then
  107.      r = GetWindowWord(hwnd, GWW_ID)
  108.      sID = CLng("&H" & Hex(r))
  109.       Else
  110.      sID = Null
  111.       End If
  112.  
  113.       ' Check that window matches the search parameters:
  114.       If sWindowText Like WindowText And sClassname Like Classname Then
  115.      If IsNull(ID) Then
  116.         ' If find a match, increment counter and add handle to array:
  117.  
  118.         iFound = iFound + 1
  119.         ReDim Preserve hWndArray(0 To iFound)
  120.         hWndArray(iFound) = hwnd
  121.      ElseIf Not IsNull(sID) Then
  122.         If sID = CLng(ID) Then
  123.            ' If find a match increment counter and add handle to array:
  124.            iFound = iFound + 1
  125.            ReDim Preserve hWndArray(0 To iFound)
  126.            hWndArray(iFound) = hwnd
  127.         End If
  128.      End If
  129.       End If
  130.  
  131.       ' Get next child window:
  132.       hwnd = GetWindow(hwnd, GW_HWNDNEXT)
  133.  
  134.    Loop
  135.  
  136.    ' Decrement recursion counter:
  137.    level = level - 1
  138.  
  139.    ' Return the number of windows found:
  140.    FindWindowLike = iFound
  141.    
  142.    End Function
  143.  
  144. ' Uses the Windows API to get the Windows directory
  145. ' which defaults to C:\Windows but we all know better
  146. ' don't we. Heck, everyone feels the need to put
  147. ' Windows where _they_ want it.
  148. '
  149. Function GetWindowsDir () As String
  150.     temp$ = String$(145, 0)              ' Size Buffer
  151.     X = GetWindowsDirectory(temp$, 145)  ' Make API Call
  152.     temp$ = Left$(temp$, X)              ' Trim Buffer
  153.  
  154.     If Right$(temp$, 1) <> "\" Then      ' Add \ if necessary
  155.     GetWindowsDir$ = temp$ + "\"
  156.     Else
  157.     GetWindowsDir$ = temp$
  158.     End If
  159. End Function
  160.  
  161. ' Uses the Windows API to get the Windows System directory
  162. ' which defaults to C:\Windows\System but we all know better
  163. ' don't we. Heck, everyone feels the need to put
  164. ' Windows where _they_ want it.
  165. '
  166. Function GetWindowsSysDir () As String
  167.     temp$ = String$(145, 0)                 ' Size Buffer
  168.     X = GetSystemDirectory(temp$, 145)      ' Make API Call
  169.     temp$ = Left$(temp$, X)                 ' Trim Buffer
  170.  
  171.     If Right$(temp$, 1) <> "\" Then         ' Add \ if necessary
  172.     GetWindowsSysDir$ = temp$ + "\"
  173.     Else
  174.     GetWindowsSysDir$ = temp$
  175.     End If
  176. End Function
  177.  
  178.